﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View.User
{
    public partial class frmUser : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }


        protected void FormSave(object sender, AjaxEventArgs e)
        {
            string password = this.userPassword.Text.ToString();
            string confirmPassword = this.userPasswordConfirm.Text.ToString();

            if (password == confirmPassword)
            {
                try
                {
                    string id = this.txt_userId.Text;
                    int asset = 2;

                    if (id.Equals(""))
                    {
                        using (guestsmanagementEntities context = new guestsmanagementEntities())
                        {
                            gmuser user = new gmuser();
                            user.IDUSER = this.userId.Text.ToString();
                            user.NMUSER = this.userName.Text.ToString();
                            user.NMMAIL = this.userEmail.Text.ToString();
                            user.NMPHONE = this.userPhone.Text.ToString();
                            user.IDPASSWORD = password;
                            user.FGUSER = int.Parse(this.userType.SelectedItem.Value.Trim());
                            if (this.userAsset.Checked == true)
                                asset = 1;
                            user.FGENABLED = asset;
                            Controller.User.addUser(user);
                            ClearWindow(Controls);
                            this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                            FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Cadastro efetuado com sucesso!", IconCls = "", Clear2 = true });
                        }
                    }
                    else
                    {
                        using (guestsmanagementEntities context = new guestsmanagementEntities())
                        {

                            gmuser user = Controller.User.getByUser(int.Parse(id));
                            user.IDUSER = this.userId.Text.ToString();
                            user.NMUSER = this.userName.Text.ToString();
                            user.NMMAIL = this.userEmail.Text.ToString();
                            user.NMPHONE = this.userPhone.Text.ToString();
                            user.IDPASSWORD = password;
                            user.FGUSER = int.Parse(this.userType.SelectedItem.Value.Trim());
                            if (this.userAsset.Checked == true)
                                asset = 1;
                            Controller.User.updateUser(user);
                            this.winUser.Hide();
                            ClearWindow(Controls);
                            this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                            Ext.Msg.Alert("Alerta!", "Usuário <br>" + user.NMUSER + " </br> atualizado com sucesso!").Show();
                            FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Usuário atualizado com sucesso!", IconCls = "", Clear2 = true });

                        }
                    }
                }
                catch
                {
                        Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
                }
            }
            else
            {
                Ext.Msg.Alert("Alerta!", "Senha digitadas não conferem!").Show();
            }
        }

        public void ShowUser()
        {
            this.winUser.Show();
        }

        public void SetUser(gmuser user)
        {
            int type = Convert.ToInt32(user.FGUSER);
            this.txt_userId.Text = user.CDUSER.ToString();
            this.userId.Text = user.IDUSER.ToString();
            this.userName.Text = user.NMUSER;
            this.userEmail.Text = user.NMMAIL;
            this.userPhone.Text = user.NMPHONE;
            this.userPassword.Text = user.IDPASSWORD;
            this.userPasswordConfirm.Text = user.IDPASSWORD;
            this.userType.SelectedItem.Value = type.ToString();
            this.userAsset.Checked = false;
            if(user.FGENABLED == 1)
                this.userAsset.Checked = true;
        }

        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winUser.Hide();
            ClearWindow(Controls);
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).Value = null;
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                ClearWindow(c.Controls);
            }
        }
    }
}